/*******************************************************************************
 * Copyright 2013 Mindaugas Idzelis
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package incontrol.bf3.protocol.events.datatype;

import java.util.List;

import com.google.common.base.Objects;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;

public class TeamScores {

	public final List<Integer> scores;
	public final int goalScore;

	public TeamScores(List<Integer> scores, int goalScore) {
		super();
		this.scores = scores;
		this.goalScore = goalScore;
	}

	public static TeamScores parse(String[] words) {
		int wordCur = 0;
		int teams = Integer.parseInt(words[wordCur++]);
		List<Integer> scores = Lists.newArrayList();
		for (int i = 0; i < teams; i++) {
			try {
				scores.add(Integer.parseInt(words[wordCur++]));
			} catch (Exception e) {
				scores.add(0);
			}
		}
		int scoreAtFin = Integer.parseInt(words[wordCur++]);
		return new TeamScores(ImmutableList.copyOf(scores), scoreAtFin);
	}

	@Override
	public String toString() {
		return Objects.toStringHelper(this).add("scores", scores)
				.add("goalScore", goalScore).toString();
	}

	@Override
	public int hashCode() {
		return Objects.hashCode(scores, goalScore);
	}

	@Override
	public boolean equals(Object object) {
		if (object instanceof TeamScores) {
			TeamScores that = (TeamScores) object;
			return Objects.equal(this.scores, that.scores)
					&& Objects.equal(this.goalScore, that.goalScore);
		}
		return false;
	}

	public List<Integer> getScores() {
		return scores;
	}

	public int getGoalScore() {
		return goalScore;
	}

}
